/**
 * 
 */
package com.mywind.service.core.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywind.dao.core.FunctionDao;
import com.mywind.dao.core.RoleDao;
import com.mywind.dao.core.UserDao;
import com.mywind.entity.core.Function;
import com.mywind.entity.core.Role;
import com.mywind.entity.core.User;
import com.mywind.service.core.IUserServ;

/**
 * @author A01162
 * 
 */
@Service
public class UserServ implements IUserServ {
	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private FunctionDao functionDao;

	@Override
	public List<User> findUserByAccPwd(String account, String password) {
		return userDao.findUserByAccPwd(account, password);
	}

	@Override
	public List<Function> findFuncsByUserId(int userid) {
		List<Integer> roleids = new ArrayList<Integer>();
		List<Role> rolelist = roleDao.getRolesSByUserId(userid);
		if (rolelist != null) {
			for (int i = 0; i < rolelist.size(); i++) {
				roleids.add(rolelist.get(i).getId());
			}
			return functionDao.getFuncsByRoles(roleids);
		}
		return null;
	}

	@Override
	public List<User> findAllUsers() {
		return userDao.findAllUsers();
	}

	@Override
	public User findUserById(int id) {
		List<User> users = userDao.findUserById(id);
		if (users != null && users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@Override
	public User findUserByAccount(String account) {
		List<User> users = userDao.findUserByAccount(account);
		if (users != null && users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void updateUser(User user) {
		userDao.updateUser(user);
	}
	
	@Override
	public void updateCompanyCodeById(User user) {
		userDao.updateCompanyCodeById(user);
	}

	@Override
	public void deleteUserById(Date lastModifyTime, String lastModifyUser,
			int id) {
		userDao.deleteUserById(lastModifyTime, lastModifyUser, id);
	}

	@Override
	public void deleteUsersByIds(Date lastModifyTime, String lastModifyUser,
			List<Integer> ids) {
		userDao.deleteUsersByIds(lastModifyTime, lastModifyUser, ids);
	}

	@Override
	public void insertUser(User user) {
		userDao.insertUser(user);
	}
}
